package com.sanqing.main;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import javax.swing.*;

import com.sanqing.dao.Dao;
import com.sanqing.iframe.*;

/**
 * 菜单和工具栏操作的集中管理类 包含系统中所有菜单项和工具栏按钮的操作定义
 */
public class MenuActions {
	// 使用静态初始化块初始化Action对象
	static {
		initActions();
	}

	// 基础数据维护 - 读者信息管理
	public static Action READER_ADD;
	public static Action READER_MODIFY;

	// 基础数据维护 - 图书类别管理
	public static Action BOOKTYPE_ADD;
	public static Action BOOKTYPE_MODIFY;

	// 基础数据维护 - 图书信息管理
	public static Action BOOK_ADD;
	public static Action BOOK_MODIFY;

	// 新书订购管理
	public static Action NEWBOOK_ORDER;
	public static Action NEWBOOK_CHECK_ACCEPT;

	// 借阅管理
	public static Action BORROW;
	public static Action GIVE_BACK;
	public static Action BOOK_SEARCH;
	public static Action BOOK_RESERVE;

	// 系统维护 - 用户管理
	public static Action USER_ADD;
	public static Action USER_MODIFY;

	// 系统维护 - 退出系统
	public static Action EXIT;

	// 读者
	// 新书推荐
	public static Action Book_Recommend;

	public static Action RECORD_CHECK;

	public static Action R_BORROW;
	public static Action R_GIVE_BACK;
	public static Action R_BOOK_SEARCH;

	public static Action R_BOOK_INFO;
	public static Action BOOK_INFO;

	public static Action R_BOOK_RESERVE;
	
	public static Action R_reserve_revoke;
	public static Action reserve_revoke;

	/**
	 * 初始化所有Action对象
	 */
	private static void initActions() {
		// 基础数据维护 - 读者信息管理
		READER_ADD = new AbstractAction("读者信息添加") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new ReaderAddFrame();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};

		READER_MODIFY = new AbstractAction("读者信息删改") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new ReadModFrame();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};

		// 基础数据维护 - 图书类别管理
		BOOKTYPE_ADD = new AbstractAction("图书类别信息添加") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new BooktypeAddFrame();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};

		BOOKTYPE_MODIFY = new AbstractAction("图书类别信息删改") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new BooktypeModFrame();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};

		// 基础数据维护 - 图书信息管理
		BOOK_ADD = new AbstractAction("图书信息添加") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new BookAddIFrame();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};

		BOOK_MODIFY = new AbstractAction("图书信息删改") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new BookModiAndDelIFrame();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};
		BOOK_INFO = new AbstractAction("图书信息") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new BookInfoFrame();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};
		R_BOOK_INFO = new AbstractAction("图书信息") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new BookInfoFrame();
				ReaderFrame.addIFame(frame);
				frame.setVisible(true);
			}
		};

		// 新书订购管理
		NEWBOOK_ORDER = new AbstractAction("新书订购") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new NewBookorderFrame();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};

		NEWBOOK_CHECK_ACCEPT = new AbstractAction("验收新书") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new NewBookcheckFrame();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};

		// 借阅管理
		BORROW = new AbstractAction("借阅图书") {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (isTimeoutReturnBook(BookLoginIFrame.getOperator().getISBN())) {
					JOptionPane.showMessageDialog(null, "有超时未归还的图书,无法借阅", "提示", JOptionPane.ERROR_MESSAGE);
				} else if (isMaxBorrowBook(BookLoginIFrame.getOperator().getISBN())) {
					JOptionPane.showMessageDialog(null, "当前已是可借书的最大值，请还书后重试", "提示", JOptionPane.ERROR_MESSAGE);
				} else {
					JInternalFrame frame = new BorrowFrame();
					Library.addIFame(frame);
					frame.setVisible(true);
				}

			}
		};
		R_BORROW = new AbstractAction("借阅图书") {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (isTimeoutReturnBook(BookLoginIFrame.getOperator().getISBN())) {
					JOptionPane.showMessageDialog(null, "有超时未归还的图书,无法借阅", "提示", JOptionPane.ERROR_MESSAGE);
				} else if (isMaxBorrowBook(BookLoginIFrame.getOperator().getISBN())) {
					JOptionPane.showMessageDialog(null, "当前已是可借书的最大值，请还书后重试", "提示", JOptionPane.ERROR_MESSAGE);
				} else {
					JInternalFrame frame = new BorrowFrame();
					ReaderFrame.addIFame(frame);
					frame.setVisible(true);
				}

			}
		};
		BOOK_RESERVE = new AbstractAction("图书预约") {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (isTimeoutReturnBook(BookLoginIFrame.getOperator().getISBN())) {
					JOptionPane.showMessageDialog(null, "有超时未归还的图书,无法预约", "提示", JOptionPane.ERROR_MESSAGE);
				}

				else {
					JInternalFrame frame = new ReserveIFrame();
					Library.addIFame(frame);
					frame.setVisible(true);
				}
			}
		};
		R_BOOK_RESERVE = new AbstractAction("图书预约") {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (isTimeoutReturnBook(BookLoginIFrame.getOperator().getISBN())) {
					JOptionPane.showMessageDialog(null, "有超时未归还的图书,无法预约", "提示", JOptionPane.ERROR_MESSAGE);
				}

				else {
					JInternalFrame frame = new ReserveIFrame();
					ReaderFrame.addIFame(frame);
					frame.setVisible(true);
				}
			}
		};
		R_reserve_revoke = new AbstractAction("我的预约") {
            @Override
            public void actionPerformed(ActionEvent e) {
            	if(isReserveBook(BookLoginIFrame.getOperator().getISBN()))
            	{
                JInternalFrame frame = new RevokeReserveFrame();
                ReaderFrame.addIFame(frame);
                frame.setVisible(true);
            	}
            	else
            	{
			   		JOptionPane.showMessageDialog(null,"没有预约过图书","提示",JOptionPane.ERROR_MESSAGE);

            	}
            }
        };
		reserve_revoke = new AbstractAction("我的预约") {
            @Override
            public void actionPerformed(ActionEvent e) {
            	if(isReserveBook(BookLoginIFrame.getOperator().getISBN()))
            	{
                JInternalFrame frame = new RevokeReserveFrame();
                Library.addIFame(frame);
                frame.setVisible(true);
            	}
            	else
            	{
			   		JOptionPane.showMessageDialog(null,"没有预约过图书","提示",JOptionPane.ERROR_MESSAGE);

            	}
            }
        };
		GIVE_BACK = new AbstractAction("归还图书") {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (isHaveBorrowBook(BookLoginIFrame.getOperator().getISBN())) {
					JOptionPane.showMessageDialog(null, "没有要归还的图书", "提示", JOptionPane.WARNING_MESSAGE);
				} else {
					JInternalFrame frame = new GiveBackFrame();
					Library.addIFame(frame);
					frame.setVisible(true);
				}
			}
		};
		R_GIVE_BACK = new AbstractAction("归还图书") {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (isHaveBorrowBook(BookLoginIFrame.getOperator().getISBN())) {
					JOptionPane.showMessageDialog(null, "没有要归还的图书", "提示", JOptionPane.WARNING_MESSAGE);
				} else {
					JInternalFrame frame = new GiveBackFrame();
					ReaderFrame.addIFame(frame);
					frame.setVisible(true);
				}
			}
		};

		BOOK_SEARCH = new AbstractAction("图书搜索") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new BookSearchFrame();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};
		R_BOOK_SEARCH = new AbstractAction("图书搜索") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new BookSearchFrame();
				ReaderFrame.addIFame(frame);
				frame.setVisible(true);
			}
		};

		// 系统维护 - 用户管理
		USER_ADD = new AbstractAction("添加用户信息") {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 显示密码验证对话框
				PasswordDialog dialog = new PasswordDialog(
						(JFrame) SwingUtilities.getWindowAncestor((Component) e.getSource()));
				if (dialog.showDialog()) { // 验证通过
					JInternalFrame frame = new UserFrame();
					Library.addIFame(frame);
					frame.setVisible(true);
				}
			}
		};

		USER_MODIFY = new AbstractAction("用户信息修改") {
			@Override
			public void actionPerformed(ActionEvent e) {
				// 显示密码验证对话框
				PasswordDialog dialog = new PasswordDialog(
						(JFrame) SwingUtilities.getWindowAncestor((Component) e.getSource()));
				if (dialog.showDialog()) { // 验证通过
					JInternalFrame frame = new UserModFrame();
					Library.addIFame(frame);
					frame.setVisible(true);
				}
			}
		};

		// 新书推荐
		Book_Recommend = new AbstractAction("图书荐购") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new NewBookRecommendFrame();
				ReaderFrame.addIFame(frame);
				frame.setVisible(true);
			}
		};
		RECORD_CHECK = new AbstractAction("验收记录") {
			@Override
			public void actionPerformed(ActionEvent e) {
				JInternalFrame frame = new RecordCheck();
				Library.addIFame(frame);
				frame.setVisible(true);
			}
		};

		// 系统维护 - 退出系统
		EXIT = new AbstractAction("退出系统") {
			@Override
			public void actionPerformed(ActionEvent e) {
				int confirm = JOptionPane.showConfirmDialog(null, "确定要退出系统吗?", "退出确认", JOptionPane.YES_NO_OPTION);
				if (confirm == JOptionPane.YES_OPTION) {
					System.exit(0);
				}
			}
		};
	}
	private static boolean isReserveBook(String username)
    {
    	int isReserveBook1=(Dao.selectReserve("select * from tb_reserve where readerISBN='"+username+"'").size());
    	if(isReserveBook1!=0) return true;
    	return false;
    }

	private static boolean isHaveBorrowBook(String username) {
		int haveBorrowBook = Dao.selectBorrow("select * from tb_borrow where readerISBN='" + username + "'and isback=0")
				.size();
		if (haveBorrowBook != 0)
			return false;
		return true;
	}

	private static boolean isTimeoutReturnBook(String username) {
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		String nowDate = dateTimeFormatter.format(LocalDate.now());
		int es = Dao.selectBookTimeout(username, nowDate).size();
		if (es == 0)
			return false;
		return true;
	}

	private static boolean isMaxBorrowBook(String username) {
		int hadBorrowBook = Dao.selectBorrow("select * from tb_borrow where readerISBN='" + username + "' and isback=0")
				.size();
		int maxBorrowBook = Integer
				.parseInt(Dao.selectOperator("select * from tb_operator where R_ISBN='" + username + "'").getMaxNum());
		int canBorrowBook = maxBorrowBook - hadBorrowBook;
		if (canBorrowBook == 0)
			return true;
		return false;
	}

	// 私有构造函数，防止实例化
	private MenuActions() {
	}
}